
package com.efast.cafe.commponent.statemach.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.commponent.statemach.bean.TaskParamBean;
import com.efast.cafe.commponent.statemach.service.IStatemMachAdapterService;
import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;

@Controller
@RequestMapping("/statemach")
public class StateMachController {

	@Autowired
	private IStatemMachAdapterService statemMachAdapterService;
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param taskParam
	 * @return
	 */
	@RequestMapping(value = "/handle/{type}")
	public @ResponseBody ResultBean taskHandle(HttpServletRequest request, HttpServletResponse response,
			@Validated @RequestBody TaskParamBean taskParam, 
		//	@PathVariable("confId") Integer confId,
			@PathVariable("type") String htype) {
		ResultBean result = new ResultBean(true, null);
		try {
			if(taskParam.getTaskId()==null || taskParam.getTaskId()==0){
				statemMachAdapterService.beginAdapter(taskParam, htype);
			}else{
				statemMachAdapterService.processAdapter(taskParam, htype, taskParam.getConfId(), taskParam.getTaskId());
			}
		} catch (Exception e) {
			e.printStackTrace();
            result.setSuccess(false);
            if (e instanceof ServiceException) {
            	ServiceException ex = (ServiceException)e;
                result.setErrorCode(ex.getCode());
                result.setMessage(ex.getMessage());
                result.setMsgParams(ex.getParams());
            } else {
                result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
            }
		}
		return result;
	}
}
